package com.bmob.lover.ui.fragment

import android.util.SparseArray
import android.view.KeyEvent
import android.view.View
import androidx.drawerlayout.widget.DrawerLayout
import com.bmob.lover.BR
import com.bmob.lover.databinding.FragmentMainBinding
import com.bmob.lover.ui.fragment.base.BaseFragment
import com.bmob.lover.viewmodel.state.MainViewModel
import com.theone.mvvm.ext.addParams
import com.theone.mvvm.ext.qmui.updateStatusBarMode

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-06-29 09:41
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class MainFragment : BaseFragment<MainViewModel, FragmentMainBinding>() {

    override fun isExitPage(): Boolean = true

    override fun isNeedChangeStatusBarMode(): Boolean = true

    override fun isStatusBarLightMode(): Boolean = getViewModel().isDrawerOpened.get()

    override fun showTopBar(): Boolean = false

    override fun translucentFull(): Boolean = true

    override fun initView(root: View) {
        childFragmentManager.beginTransaction()
            .add(getDataBinding().flContent.id, Index2Fragment())
            .add(getDataBinding().flSlide.id, WeFragment())
            .commit()
    }

    override fun SparseArray<Any>.applyBindingParams() {
        addParams(BR.event, DrawerEventHandler())
    }

    override fun createObserver() {
        mEventVm.isToOpenOrCloseDrawer().observe(this, androidx.lifecycle.Observer {
            if (it != getViewModel().isDrawerOpened.get()) {
                updateStatusBarMode(it)
                getViewModel().openDrawer.value = it
                getViewModel().isDrawerOpened.set(it)
            }
        })
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && getViewModel().isDrawerOpened.get()) {
            mEventVm.requestToOpenOrCloseDrawer(false)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    inner class DrawerEventHandler : DrawerLayout.SimpleDrawerListener() {

        override fun onDrawerOpened(drawerView: View) {
            super.onDrawerOpened(drawerView)
            mEventVm.requestToOpenOrCloseDrawer(true)
        }

        override fun onDrawerClosed(drawerView: View) {
            super.onDrawerClosed(drawerView)
            mEventVm.requestToOpenOrCloseDrawer(false)
        }

    }


}